package photobox.manage

import grails.transaction.Transactional
import photobox.exception.NotFoundException
import photobox.photo.PhotoPaper
import photobox.photo.PhotoPaperPrice
import photobox.photo.PhotoSize
import photobox.utils.HQLUtils

@Transactional
class PhotoPaperPriceService {

    def list(searchParam, pageParam, sortParam) {
        def hql = ""
        hql += " from PhotoPaperPrice A where 1=1 "
        def params = [:]
        if (searchParam.id) {
            hql += " and " + HQLUtils.whereCondition("A.id", searchParam.id.opt, "id")
            params.id = searchParam.id.data
        }
        if (searchParam.PhotoPaper) {
            hql += " and " + HQLUtils.whereCondition("A.PhotoPaper", searchParam.PhotoPaper.opt, "PhotoPaper")
            params.PhotoPaper = searchParam.PhotoPaper.data
        }
        if (searchParam.PhotoSize) {
            hql += " and " + HQLUtils.whereCondition("A.PhotoSize", searchParam.PhotoSize.opt, "PhotoSize")
            params.PhotoSize = searchParam.PhotoSize.data
        }
        if (searchParam.price) {
            hql += " and " + HQLUtils.whereCondition("A.price", searchParam.price.opt, "price")
            params.price = searchParam.price.data
        }

        def sortHql = ""
        if (sortParam.sort) {
            sortHql = " order by A.${sortParam.sort} ${sortParam.order} "
        }
        def results =PhotoPaperPrice.executeQuery("select distinct A " + hql + sortHql, params,
                [max: pageParam.pageSize, offset: pageParam.offset])
        def count =PhotoPaperPrice.executeQuery("select count(distinct A.id) " + hql, params)

        return [rs:results, total:count[0]]
    }
    def createPhotoPaperPrice(params){
        def photoPaperPrice = new PhotoPaperPrice()

        def photoSize = PhotoSize.get(params.photoSizeId)
        def photoPaper = PhotoPaper.get(params.photoPaperId)
        photoPaperPrice.photoSize = photoSize
        photoPaperPrice.photoPaper = photoPaper
        photoPaperPrice.price =Float.parseFloat( params.price)

        if(params.isDeal=="on"){
            photoPaperPrice.isDeal = photoPaperPrice.ISDEAL_YES
        }else{
            photoPaperPrice.isDeal = photoPaperPrice.ISDEAL_NO
        }
        if(!photoPaperPrice.save()){
            println(photoPaperPrice.errors)
        }
    }
    def updatePhotoPaperPrice(params){
        def photoPaperPrice = PhotoPaperPrice.get(params.id)
        def photoSize = PhotoSize.get(params.photoSizeId)
        def photoPaper = PhotoPaper.get(params.photoPaperId)
        photoPaperPrice.photoSize = photoSize
        photoPaperPrice.photoPaper = photoPaper
        photoPaperPrice.price =Float.parseFloat( params.price)
        if(params.isDeal=="0"){
            photoPaperPrice.isDeal = photoPaperPrice.ISDEAL_YES
        }else{
            photoPaperPrice.isDeal = photoPaperPrice.ISDEAL_NO
        }
        if(!photoPaperPrice.save()){
            println(photoPaperPrice.errors)
        }
    }
    def deletePhotoPaperPrice(params){
        def photoPaperPrice = PhotoPaperPrice.get(params.id)
        if (!photoPaperPrice) {
            throw new NotFoundException("没有找到对应的价格")
        }
        photoPaperPrice.delete()
    }
}
